<template>
	<div class="login-form">
		<a-form-model ref="loginForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
			<a-form-model-item ref="name" prop="name">
				<a-input v-model="form.name">
					<a-icon slot="prefix" type="user" />
				</a-input>
			</a-form-model-item>
			<a-form-model-item ref="password" prop="password">
				<a-input-password v-model="form.password">
					<a-icon slot="prefix" type="unlock" />
				</a-input-password>
			</a-form-model-item>
			<a-form-model-item :wrapper-col="wrapperCol">
				<a-button type="primary" size="large" style="width: 100%;font-size: 20px;" @click="onSubmit" :loading="loading">
					登录
				</a-button>
			</a-form-model-item>
		</a-form-model>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				labelCol: {
					span: 6
				},
				wrapperCol: {
					span: 18
				},
				form: {
					name: '',
					password: ''
				},
				rules: {
					name: [{
						required: true,
						message: '请输入用户名',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			onSubmit() {
				let self = this;
				this.$refs.loginForm.validate(valid => {
					if (valid) {
						self.loading = true;
						self.$store.dispatch("Login", self.form).then((res) => {
							if (res.code == 0) {
								self.loading = false;
								this.$router.replace("/");
							} else {
								this.$message.error(res.status)
								self.loading = false;
							}
						}).catch(() => {
							self.loading = false;
						});
					} else {
						return false;
					}
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	.login-form {
		width: 100%;
		height: 100%;
		padding-top: 120px;
		box-sizing: border-box;
		padding-left: 136px;

		.ant-form-item {
			color: red;
		}
	}
</style>
